package com.atguigu.gulimail.ware.service.impl;

import com.atguigu.common.constant.PurchaseItemStatus;
import com.atguigu.common.constant.PurchaseStatus;
import com.atguigu.gulimail.ware.dao.WmsPurchaseDetailDao;
import com.atguigu.gulimail.ware.entity.WmsPurchaseDetailEntity;
import com.atguigu.gulimail.ware.entity.WmsWareSkuEntity;
import com.atguigu.gulimail.ware.service.WmsWareSkuService;
import com.atguigu.gulimail.ware.to.MergeTo;
import com.atguigu.gulimail.ware.to.PurchaseDoneVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.Query;

import com.atguigu.gulimail.ware.dao.WmsPurchaseDao;
import com.atguigu.gulimail.ware.entity.WmsPurchaseEntity;
import com.atguigu.gulimail.ware.service.WmsPurchaseService;
import org.springframework.transaction.annotation.Transactional;


@Service("wmsPurchaseService")
public class WmsPurchaseServiceImpl extends ServiceImpl<WmsPurchaseDao, WmsPurchaseEntity> implements WmsPurchaseService {

    @Autowired
    private WmsPurchaseDetailDao purchaseDetailDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<WmsPurchaseEntity> page = this.page(
                new Query<WmsPurchaseEntity>().getPage(params),
                new QueryWrapper<WmsPurchaseEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPageUnreceive(Map<String, Object> params) {

        IPage<WmsPurchaseEntity> page = this.page(
                new Query<WmsPurchaseEntity>().getPage(params),
                new QueryWrapper<WmsPurchaseEntity>().eq("status", 0).or().eq("status", 1)
        );
        if (page != null) {
            return new PageUtils(page);
        }
        return null;
    }

    @Transactional
    @Override
    public void mergeItem2Purchase(MergeTo mergeTo) {
        if (null == mergeTo.getPurchaseId() || 0 == mergeTo.getPurchaseId()) {
            WmsPurchaseEntity purchaseEntity = new WmsPurchaseEntity();
            purchaseEntity.setStatus(PurchaseStatus.CREATED.getCode());
            baseMapper.insert(purchaseEntity);
            mergeTo.setPurchaseId(purchaseEntity.getId());
        } else {
            final WmsPurchaseEntity wmsPurchaseEntity =
                    baseMapper.selectById(mergeTo.getPurchaseId());
            if (wmsPurchaseEntity.getStatus() != PurchaseStatus.CREATED.getCode() &&
                    wmsPurchaseEntity.getStatus() != PurchaseStatus.ASSIGNED.getCode()
            ) {
                throw new RuntimeException("状态不匹配");
            }
        }
        mergeTo.getItems().forEach(item -> {
            WmsPurchaseDetailEntity purchaseDetailEntity = new WmsPurchaseDetailEntity();
            purchaseDetailEntity.setId(item);
            purchaseDetailEntity.setPurchaseId(mergeTo.getPurchaseId());
            purchaseDetailEntity.setStatus(PurchaseItemStatus.ASSIGNED.getCode());
            purchaseDetailDao.updateById(purchaseDetailEntity);
        });
        WmsPurchaseEntity purchaseEntity = new WmsPurchaseEntity();
        purchaseEntity.setId(mergeTo.getPurchaseId());
        purchaseEntity.setUpdateTime(new Date());
        baseMapper.updateById(purchaseEntity);
    }

    @Transactional
    @Override
    public void received(List<Long> ids) {
        for (Long id : ids) {
            WmsPurchaseEntity purchaseEntity = baseMapper.selectById(id);
            if (null == purchaseEntity) {
                throw new RuntimeException("采购单不存在");
            }
            if (purchaseEntity.getStatus() != PurchaseStatus.CREATED.getCode() && purchaseEntity.getStatus() != PurchaseStatus.ASSIGNED.getCode()) {
                throw new RuntimeException("状态不匹配");
            }
            WmsPurchaseEntity updateEntity = new WmsPurchaseEntity();
            updateEntity.setId(id);
            updateEntity.setStatus(PurchaseStatus.RECEIVE.getCode());
            updateEntity.setUpdateTime(new Date());
            updateById(purchaseEntity);
            // 修改 采购需求单状态
            List<WmsPurchaseDetailEntity> purchaseDetailEntities =
                    purchaseDetailDao.selectList(new QueryWrapper<WmsPurchaseDetailEntity>().eq(
                            "purchase_id", id));
            purchaseDetailEntities.forEach(item -> {
                WmsPurchaseDetailEntity updateDetailEntity = new WmsPurchaseDetailEntity();
                updateDetailEntity.setId(item.getId());
                updateDetailEntity.setStatus(PurchaseItemStatus.RECEIVE.getCode());
                purchaseDetailDao.updateById(updateDetailEntity);
            });
        }
    }

    @Autowired
    private WmsWareSkuService wareSkuService;

    @Transactional
    @Override
    public void done(PurchaseDoneVo purchaseDoneVo) {
//        final WmsPurchaseEntity wmsPurchaseEntity =
//                this.baseMapper.selectById(purchaseDoneVo.getId());
        boolean allSuccess = true;
        //先更新需求
        for (PurchaseDoneVo.ItemsBean itemsBean : purchaseDoneVo.getItems()) {
            WmsPurchaseDetailEntity purchaseDetailEntity = new WmsPurchaseDetailEntity();
            purchaseDetailEntity.setId(itemsBean.getItemId());
            purchaseDetailEntity.setStatus(itemsBean.getStatus());
            purchaseDetailDao.updateById(purchaseDetailEntity);

            if (itemsBean.getStatus() == PurchaseItemStatus.FINISH.getCode()) {
                final WmsPurchaseDetailEntity wmsPurchaseDetailEntity =
                        purchaseDetailDao.selectById(itemsBean.getItemId());

                WmsWareSkuEntity wareSkuEntity = new WmsWareSkuEntity();
                wareSkuEntity.setSkuId(wmsPurchaseDetailEntity.getSkuId());
                wareSkuEntity.setWareId(wmsPurchaseDetailEntity.getWareId());
                wareSkuEntity.setStock(wmsPurchaseDetailEntity.getSkuNum());
                wareSkuService.addStock(wareSkuEntity);
            }

            if (itemsBean.getStatus() == PurchaseItemStatus.HASERROR.getCode()) {
                allSuccess = false;
            }
        }
        WmsPurchaseEntity updateEntity = new WmsPurchaseEntity();
        updateEntity.setId(purchaseDoneVo.getId());
        updateEntity.setStatus(allSuccess ? PurchaseStatus.FINISH.getCode() :
                PurchaseStatus.HASERROR.getCode());
        updateEntity.setUpdateTime(new Date());
        this.updateById(updateEntity);
    }
}